<%@page import="org.shop.entity.security.MyUserDetail" %>
<%@page import="org.shop.util.ObjUtil" %>
<%@page import="org.springframework.security.core.context.SecurityContextImpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    
    String classId = request.getParameter("classId");
    String className = request.getParameter("className");

    SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
    MyUserDetail ud = null;
    Byte state = 0;
    if (ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        state = ud.getState();
    }
%>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0">
    <title>详情</title>
    <link rel="stylesheet" href="<%=basePath%>assets/css/public.css">
    <link rel="stylesheet" href="<%=basePath%>assets/css/page.css">
    <link rel="stylesheet" href="<%=basePath%>assets/font-awesome-4.7.0/css/font-awesome.min.css">
    <script src="<%=basePath%>assets/js/jquery-2.1.1.min.js"></script>
    <script src="<%=basePath%>assets/js/fontSize.js"></script>
    <link href="<%=basePath%>/assets/layer/mobile/need/layer.css" rel="stylesheet" type="text/css">
	<script src="<%=basePath%>/assets/layer/mobile/layer.js"></script>
    <style>

        body{
            padding-top: 0.8rem;
        }
        .goods {
            margin: 0.2rem 0;
        }

        .goods_list {
            padding: 0 0.2rem;
        }

        .photo {
            width: 0.8rem;
        }

        .photo img {
            width: 0.7rem;
            height: 0.7rem;
            border-radius: 50%;
        }

        .info {
            line-height: 0.36rem;
            margin-top: 0.02rem;
            font-size: 0.26rem;
        }
        .time {
            color: #989898;
            font-size: 0.24rem;
        }
        .goods_introduce {
            line-height: 0.4rem;
            margin: 0.2rem 0;
            font-size: 0.28rem;
        }

        .goods_img img {
            width: 2rem;
            height: 2.4rem;
            margin-right: 0.12rem;
        }

        .operation {
            line-height: 0.8rem;
            text-align: right;
            font-size: 0.26rem;
            margin-top: 0.2rem;
        }

        .operation_btn {
            padding:0.1rem;
            position: relative;
            margin-left: 0.1rem;
        }

        .operation_btn i {
            width:0.4rem;
            height:0.4rem;
            display: inline-block;
            position: absolute;
            top:0.04rem;
            background-size: cover;
            margin-right: 0.06rem;
        }
        .operation_btn span{
            margin-left: 0.5rem;
        }
        .favorite_o{
            background-image: url("<%=basePath%>assets/images/favorite.png");
        }
        .active_favorite{
            background-image: url("<%=basePath%>assets/images/active_favorite.png");
        }
        .info_img{
            background-image: url("<%=basePath%>assets/images/info_img.png");
        }
    </style>
</head>
<body>
<input id="basePath" type="hidden" value="<%=basePath%>">
<div class="header">
    <a class="returnInfo" href="#" onclick="window.history.go(-1)"><i class="fa fa-angle-left"></i></a>
    <%=className%>讨论区
</div>
<div class="goods_list"></div>
</body>
<script>
	var geting = false;//是否加载信息，默认为false
	var currentPage = 1;//当前页
	var pageSize = 10;//每页数据数
	
	$(function () {
		getData();
	    
	    $(".header_ul a").click(function () {
	        $(".header_ul a").removeClass("active");
	        $(this).addClass("active");
	    })
	    $(".look_more").click(function () {
	        $(".label_box").toggleClass("max_height")
	    })
// 	    $(".praise").click(function () {
// 	        $(this).find("i").toggleClass("fa-heart-o");
// 	        $(this).find("i").toggleClass("fa-heart")
// 	    });
	    
	    $(window).scroll(function () {
			if (geting) {
	        	return;
	        }
	        var scrollTop = $(this).scrollTop();
	        var scrollHeight = $(document).height();
	        var windowHeight = $(this).height();
	        if (scrollTop + windowHeight == scrollHeight) {
	        	currentPage++;
	            getData();
	        }
	        
	    });
	    
	  	//赞||取消赞
		$('.praise').click(function () {
			
			
			if("<%=securityContextImpl%>" == "null"){
	    		window.location.href = "<%=basePath%>app/appLogin2";
	    		return;
	    	}
			
			var state = 1;
			
			var targetId = $(this).siblings('input').val();
	    	$.ajax({
		    	url: "<%=basePath%>app/addPraise",
		    	async:false,
		        type: 'post',
		        data: {"postId":postId,"targetId":targetId,"praiseType":1},
		        dataType: 'json',
		        success: function (data) {
		        	if(data.state==1){
		        		state = 1;
		        	}
    	        },error:function (){
    	        }
			});
	    	
	    	if(state == 1){
	    		if($(this).find("i").attr("class") == "active_favorite"){
		    		$(this).find('span').html(Number($(this).find('span').text()) - 1);
	    		}else{
		    		$(this).find('span').html(Number($(this).find('span').text()) + 1);
	    		}
	    		$(this).find("i").toggleClass("favorite_o");
		        $(this).find("i").toggleClass("active_favorite")
	    	}
		});
	    
	})
    function getData() {
    	geting = true;
    	$.ajax({
			url : "<%=basePath%>app/getPostMessageList",
			async:false,
			dataType : "json",
			type : "post",
			data : {
				"currentPage" : currentPage,
				"pageSize" : pageSize,
				"classId" : <%=classId%>
			},
			success:function(data) {
				//判断是否有数据
				if(data=='[]'||data==''||data==null){
// 					$(".goods_list").append("暂无数据~");
				}else{
					$.each(data, function (i, item) {
						var html = '<div class="goods border-bottom-1">'+
					        '<div class="business_info clearFirx">'+
					            '<div class="photo float_l">'+
					                '<img src="<%=basePath%>app/getGoodsPhotoByName?goodsImage='+item.userPhoto+'" alt="">'+
					            '</div>'+
					            '<div class="info float_l">'+
					                '<span class="name">'+item.userName+'</span><br>'+
					                '<span class="time">'+item.createDate+'</span>'+
					            '</div>'+
					        '</div>'+
					        '<div class="goods_detail">'+
					        '<a href="<%=basePath%>app/getPostDetails?commentsId='+item.commentsId+'">'+
						            '<p class="goods_introduce">'+item.content+'</p>'+
						            '<div class="goods_img">';
						            	if(item.contentImg != null && item.contentImg != ""){
							            	var contentImgArr = item.contentImg.split(",");
							            	for(var i=0;i<contentImgArr.length;i++){
							            		html+='<img src="<%=basePath%>app/getGoodsPhotoByName?goodsImage='+contentImgArr[i]+'" alt="">';
							            	}
						            	}
						            html+='</div>'+
					            '</a>'+
					            '<div class="operation">'+
					                '<span class="operation_btn praise">'
					                	if(<%=state%> == 1 && item.state == 1){
					                    	html+='<i class="active_favorite"></i>'
					                	}else{
					                		html+='<i class="favorite_o"></i>'
					                	}
				                    	html+='<span>'+item.praiseCount+'</span>'+
					               '</span>'+
					               '<a href="<%=basePath%>jsp/app/find/forumDetails.jsp">'+
						                '<span class="operation_btn message">'+
						                    '<i class="info_img"></i>'+
						                    '<span>'+item.answerCount+'</span>'+
						                '</span>'+
					                '</a>'+
					                '<input type="hidden" value="'+item.commentsId+'">'+
					            '</div>'+
					        '</div>'+
						'</div>';
						$(".goods_list").append(html);
						geting = false;
					})
				}
			},
			error : function(data) {
				layer.open({
				    content: '网络错误~'
				    ,skin: 'msg'
				    ,time: 2 //2秒后自动关闭
				  });
			}
		})
    }
    
</script>
</html>